/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (xianliru@163.com)
 */
package com.pig4cloud.pig.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.admin.api.dto.AreaTree;
import com.pig4cloud.pig.admin.api.dto.DeptTree;
import com.pig4cloud.pig.admin.api.entity.Area;
import com.pig4cloud.pig.admin.api.entity.SysDept;
import com.pig4cloud.pig.admin.api.vo.TreeUtil;
import com.pig4cloud.pig.admin.mapper.AreaMapper;
import com.pig4cloud.pig.admin.service.AreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 中国地区
 *
 * @author xianliru@163.com
 * @date 2020-04-23 23:17:09
 */
@Slf4j
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

    /**
     * 获取直辖市省集合
     *
     * @return
     */
    @Override
    public List<Area> getProvince() {
        QueryWrapper  wrapper = new QueryWrapper(  );
        wrapper.eq( "level", 0 );
        wrapper.orderByDesc( "pinyin" );
        return baseMapper.selectList( wrapper );
    }

    /**
     * 根据省上一级code 获取下一级集合
     *
     * @param code
     */
    @Override
    public List <Area> getChildrensByParentCode(Long code) {
        QueryWrapper  wrapper = new QueryWrapper(  );
        wrapper.eq( "parent_code", code );
        wrapper.orderByDesc( "pinyin" );
        return baseMapper.selectList( wrapper );
    }

    /**
     * 构建地区树形结构
     *
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List <AreaTree> getAreaTree() {
        QueryWrapper <Area> objectQueryWrapper = new QueryWrapper <>();
        List <Area> areas = baseMapper.selectList( objectQueryWrapper );
        return getAreaTree(areas);
    }


    /**
     * 构建地区树
     *
     * @param areas 地区
     * @return
     */
    private List<AreaTree> getAreaTree(List<Area> areas) {
        List<AreaTree> treeList = areas.stream()
                .filter(area -> !area.getAreaCode().equals(area.getParentCode()))
                .sorted( Comparator.comparing(Area::getPinyin))
                .map(area -> {
                    AreaTree node = new AreaTree();
                    node.setId(area.getId());
                    node.setParentId(area.getAreaCode().intValue());
                    node.setName(area.getName());
                    return node;
                }).collect( Collectors.toList());
        return TreeUtil.build(treeList, 0);
    }

}
